  /*!
\copyright  Copyright (c) 2020 Qualcomm Technologies International, Ltd.\n
            All Rights Reserved.\n
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\version    %%version
\file       main.c
\brief      Main application task
*/

#include <connection.h>
#include <panic.h>
#include <pio.h>
#include <ps.h>
#include <string.h>
#include <boot.h>
#include <os.h>
#include <operator.h>
#include <logging.h>
#include <app/message/system_message.h>

#include "source_a2dp_msg_handler.h"
#include "source_app_msg_handler.h"
#include "source_audio.h"
#include "source_connection_msg_handler.h"
#include "source_memory.h"
#include "source_private.h"
#include "source_states.h"
#include "source_usb.h"
#include "source_connection_mgr.h"
#include "source_ps.h"
#include "source_upgrade.h"
#include "source_main.h"
#include "source_power.h"

/* application variables */
SOURCE_TASK_DATA_T *theSource;

void source_init(void)
{
    theSource = memory_create(sizeof(SOURCE_TASK_DATA_T));

    if (theSource == NULL)
    {
        /* cannot create memory required to hold variables - check memory usage */
        Panic();
    }

    /* initialise the application variables */
    memset(theSource, 0, sizeof(SOURCE_TASK_DATA_T));

    /* Set task message handlers */
    theSource->connectionTask.handler   = connection_msg_handler;
    theSource->app_data.appTask.handler = source_app_handler;

    sourcePsInit();

    connection_mgr_init();

    /* Time critical USB setup */
    usb_time_critical_init();
}

/*! \brief Application entry point

    This function is the entry point for the application, it performs basic
    initialisation of the application task state machine and then sets
    the state to 'initialising' which will start the initialisation procedure.

    \returns Nothing. Only exits by powering down.
*/
void source_main(void)
{
    /* permit audio to stream */
    OperatorFrameworkEnable(1);

#ifdef INCLUDE_POWER_READINGS
    /* initialise power readings */
    power_init();
#endif /* INCLUDE_POWER_READINGS */

    sourceUpgradeInit();

    /* Start the message scheduler loop */
    MessageLoop();
}
